package org.quickstart.jstorm.example6.jstorm.bolt;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;

public class OddBolt extends BaseRichBolt {

  /**
   *
   */
  private static final long serialVersionUID = 1L;

  private static final Logger logger = LoggerFactory.getLogger(OddBolt.class);

  private OutputCollector collector;

  @Override
  public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    this.collector = collector;
  }

  @Override
  public void execute(Tuple input) {
    Integer kafka = input.getInteger(0);
    Integer db = input.getInteger(1);
    int sum = kafka + db;
    if (sum % 2 != 0) {
      // 奇数，进行发射
      this.collector.emit(new Values(sum));
    }
    // 发送ack信息告知spout 完成处理的消息
    this.collector.ack(input);
  }

  @Override
  public void declareOutputFields(OutputFieldsDeclarer declarer) {
    declarer.declare(new Fields("odd"));
  }

}
